//
//  UserAccountItem.swift
//  微博
//
//  Created by 段瑞金 on 16/5/15.
//  Copyright © 2016年 段瑞金. All rights reserved.
//

/*
 Swift2.0开始, 要想打印对象必须遵守CustomStringConvertible协议, 重写description属性
 Swift2.0之前, 要想打印对象必须遵守Printable协议, 重写description属性
 
 只要是继承于NSObject的对象, 只需要直接重写description属性即可
 
 */
import UIKit

class UserAccountItem: NSObject , NSCoding{

    /// 用于调用access_token，接口获取授权后的access token。
    var access_token: String?
    /// access_token的生命周期，单位是秒数。
    var expires_in: Double = -1 {
        didSet {
            expires_in = 30
            expires_DATE = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    /// 当前授权用户的UID。
    var uid: String?
    ///真正过期时间
    var expires_DATE: NSDate?
    
    
    //存储路径
    /*
    static var filepath : String {
//        get {
            //获取系统路径
            let path = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true).last!
//            RJLOG(path)
            //拼接路径
            let filePath = (path as NSString).stringByAppendingPathComponent("accountItem.plist")
            
            return filePath
//        }

    }
    */
    
    static var filepath : String = "accountItem.plist".cachesDirectory()
    

    
    //MARK:-生命周期方法
    init(dict : [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    override var description: String {
        let keys = ["access_token" , "expires_in" , "uid" , "expires_DATE"]
        //模型转字典
        let dict = dictionaryWithValuesForKeys(keys)
        //字典转字符串
        return "\(dict)"
    }
    
    ///从文件中读取一个模型时调用
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_in = aDecoder.decodeObjectForKey("expires_in") as! Double
        uid = aDecoder.decodeObjectForKey("uid") as? String
        expires_DATE = aDecoder.decodeObjectForKey("expires_DATE") as? NSDate
        
    }
    ///将模型写入模型时会调用
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expires_in, forKey: "expires_in")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(expires_DATE, forKey: "expires_DATE")
    }
    
    //MARK:-内部控制方法
    ///使用归档保存accessToken信息
     func saveUserAccountItem () -> Bool {
//        //获取系统路径
//        let path = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true).last!
//        RJLOG(path)
//        //拼接路径
//        let filePath = (path as NSString).stringByAppendingPathComponent("accountItem.plist")
        //保存
        return NSKeyedArchiver.archiveRootObject(self, toFile: UserAccountItem.filepath)
    }

    
    ///读取保存的信息
    private class func readAccountItem () -> UserAccountItem? {
//        //获取系统路径
//        let path = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true).last!
//        
//        //拼接路径
//        let filePath = (path as NSString).stringByAppendingPathComponent("accountItem.plist")
        
        let account = NSKeyedUnarchiver.unarchiveObjectWithFile(UserAccountItem.filepath) as? UserAccountItem

        //判断真正过期时间和当前时间,如果结果是升序,表示过期了
        if account?.expires_DATE?.compare(NSDate()) == NSComparisonResult.OrderedAscending {

            return nil
        }
        return account
    }
    
    
    ///提供一个判断是否读取成功的方法,也就是可以说明是否登录成功的方法
    class func userLogin() -> Bool {
        
        return readAccountItem() != nil
    }
    

}
